﻿using System;
using System.Linq;
using System.Windows.Forms;

namespace InstallWSPBuilderForms
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataBindOsType();
            TrySetOsBitness();
            DataBindVisualStudioVersion();
            DataBindVisualStudioType();
        }


        private static void DataBindBitType(ComboBox cbx)
        {
            cbx.ValueMember = "Key";
            cbx.DisplayMember = "Value";
            cbx.DataSource = Enum.GetNames(typeof(Bitness))
                .Select(a => EnumHelpers.ConvertBitTypeStringToBitEnum(a))
                .Select
                (x => new
                {
                    Key = x,
                    Value = x.FormatedBitnessString(),
                }
                ).ToList();
        }

        private void DataBindOsType()
        {
            DataBindBitType(cbxOsType);
        }

        private void DataBindVisualStudioType()
        {
            DataBindBitType(cbxVsType);
        }

        private void DataBindVisualStudioVersion()
        {
            cbxVsVersion.ValueMember = "Key";
            cbxVsVersion.DisplayMember = "Value";
            cbxVsVersion.DataSource = Enum.GetNames(typeof(VisualStudioVersion))
                .Select(a => EnumHelpers.ConvertVsStringToVsEnumVersion(a))
                .Select
                (x => new
                {
                    Key = x,
                    Value = x.GetFullVisualStudioVersion(),
                }
                ).ToList();
        }

        private bool TrySetOsBitness()
        {
            Bitness osBitness;
            try
            {
                osBitness = InstallationEngine.GetSystemBitness();
            }
            catch { return false; }
            cbxOsType.SelectedValue = osBitness;
            return true;
        }


        private void btnInstall_Click(object sender, EventArgs e)
        {
            btnInstall.Enabled = false;
            string originalText = btnInstall.Text;
            btnInstall.Text = "Installing, please wait...";
            btnInstall.Refresh();

            InstallationResult result;
            try
            {
                result = InstallationEngine.PerformInstall((Bitness)cbxOsType.SelectedValue, (VisualStudioVersion)cbxVsVersion.SelectedValue, (Bitness)cbxVsType.SelectedValue);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception was thrown with message: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                btnInstall.Enabled = true;
                return;
            }
            if (result.Success)
            {
                btnInstall.Text = "Installed";
                btnInstall.Refresh();
                MessageBox.Show(result.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show(result.Message, "Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                btnInstall.Text = originalText;
                btnInstall.Enabled = true;
            }

        }

    }
}
